package cn.iune.erp.invoice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Aspect//指定这是一个其切面
public class AccountInvoiceDaoAspect {
	
	@Autowired
	private IInvoiceService invoiceService;
	
	
	//指定切入点（增加）
    @Pointcut("execution(* cn.iune.erp.invoice.IAccountInvoiceDao.insert(..)) && args(obj)")
    public void insertPointCut(AccountInvoice obj){}
    
    //指定通知以及通知的范围
    @Transactional
    @After("insertPointCut(obj)")
    public void afterInsert(AccountInvoice obj){
//    	System.out.println("AccountInvoiceDaoAspect.afterInsert");
    	invoiceService.afterInsertInvoice(obj);
    }
    
	//指定切入点（删除）
    @Pointcut("execution(* cn.iune.erp.invoice.IAccountInvoiceDao.delete(..)) && args(fID)")
    public void deletePointCut(String fID){}
    
    //指定通知以及通知的范围
    @Transactional
    @Before("deletePointCut(fID)")
    public void beforeDelete(String fID){
//    	System.out.println("AccountInvoiceDaoAspect.beforeDelete");
    	invoiceService.beforeDeleteInvoice(fID);
    }
	//指定切入点（修改）
    @Pointcut("execution(* cn.iune.erp.invoice.IAccountInvoiceDao.update(..)) && args(obj)")
    public void updatePointCut(AccountInvoice obj){}
    
    //指定通知以及通知的范围
    @Transactional
    @Around("updatePointCut(obj)")
    public AccountInvoice aroundUpdate(JoinPoint joinPoint, AccountInvoice obj) throws Throwable{
		//更新主表合计
//    	System.out.println("AccountInvoiceDaoAspect.aroundUpdate-->>proceed()之前");
//    	System.out.println("AccountInvoiceDaoAspect.aroundUpdate-->>proceed()之前obj.getHandling()-->>"+obj.getBillTypeName()+obj.getHandling());
//    	System.out.println("AccountInvoiceDaoAspect.aroundUpdate-->>proceed()之前obj.getHandlingLabel()-->>"+obj.getBillTypeName()+obj.getHandlingLabel());

    	//java遍历实体类的属性和值
    	/*
    	Class<? extends AccountInvoice> cls = obj.getClass();  
        Field[] fields = cls.getDeclaredFields();  
        for(int i=0; i<fields.length; i++){  
            Field f = fields[i];  
            f.setAccessible(true);  
            System.out.println("属性名:" + f.getName() + " 属性值:" + f.get(obj));  
        } 
        */
        //AppSystemService.reflect(obj.getClass());
    	
    	invoiceService.beforeUpdateInvoice(obj);
        
    	//下面这一行是不能被注释掉的，否则上面切入点的update函数将不会被调用
    	System.out.println(this.getClass().getName()+"环绕通知上。。。");
    	AccountInvoice objNew = (AccountInvoice) ((ProceedingJoinPoint) joinPoint).proceed();
    	System.out.println(this.getClass().getName()+"环绕通知下。。。");
//	    System.out.println("AccountInvoiceDaoAspect.aroundUpdate-->>proceed()之后");
//	    System.out.println("AccountInvoiceDaoAspect.aroundUpdate-->>proceed()之后objNew.getHandling()-->>"+objNew.getBillTypeName()+objNew.getHandling());
//	    System.out.println("AccountInvoiceDaoAspect.aroundUpdate-->>proceed()之后objNew.getHandlingLabel()-->>"+objNew.getBillTypeName()+objNew.getHandlingLabel());
    	/*
    	Class<? extends AccountInvoice> cls2 = objNew.getClass();  
        Field[] fields2 = cls2.getDeclaredFields();  
        for(int i=0; i<fields2.length; i++){  
            Field f = fields2[i];  
            f.setAccessible(true);  
            System.out.println("属性名:" + f.getName() + " 属性值:" + f.get(objNew));  
        } 
    	*/
	    //AppSystemService.reflect(objNew.getClass());
	    
	    invoiceService.afterUpdateInvoice(objNew);

        return objNew;
    }

}
